package com.huanzhidadi.rollback.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanzhidadi.rollback.exception.BaseException;
import com.huanzhidadi.rollback.mapper.AddressMapper;
import com.huanzhidadi.rollback.model.Address;
import com.huanzhidadi.rollback.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public int insertWithoutTransactional(Address address) {
        return addressMapper.insert(address);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertWithTransactional(Address address) {
        return addressMapper.insert(address);
    }

    @Override
    public int insertException(Address address) throws BaseException {
        if (!address.getAddr().startsWith("河南")) {
            throw new BaseException("地址范围错误");
        }

        return addressMapper.insert(address);
    }
}
